var f_type = new Array();
var f_tier = new Array();

window.onload = function() {
	document.getElementById('addRessourceLink').addEventListener('click', function (event){openFrm()}, false);
	document.getElementsByTagName('form')[0].addEventListener('reset', function (event){resetFilters()}, false);
	document.getElementsByTagName('form')[0].addEventListener('submit', function (event){setInputTank()}, false);
	var lstInput = document.getElementsByTagName('input');
	for (i=0; i<lstInput.length; i++) {
		if (lstInput[i].type == 'reset') {
			lstInput[i].addEventListener('click', function(event) {
				document.getElementById('addRessource').style.display='none';
			}, false)
		}
	}
	var lstImg = document.getElementsByTagName('img');
	for (i = 0; i < lstImg.length; i++) {
		if(lstImg[i].id.indexOf('delete') == 0) {
			lstImg[i].addEventListener('click', function (event) {deleteRes(event.target)}, false);
		}
		if(lstImg[i].id.indexOf('edit') == 0) {
			lstImg[i].addEventListener('click', function (event) {editRes(event.target)}, false);
		}
	}
	var lstSpan = document.getElementsByTagName('span');
	for (s = 0; s < lstSpan.length; s++) {
		if(lstSpan[s].className == 'tiers') {
			lstSpan[s].addEventListener('click', function (event) {filtrerTank(event)}, false);
		}
		if (lstSpan[s].classList[0] == 'icone_type') {
			lstSpan[s].addEventListener('click', function (event) {filtrerTank(event)}, false);
		}
	}
}

function openFrm() {
	document.getElementById('addRessource').style.display='block';
	document.getElementsByName('frm_id')[0].value = '';
}

function deleteRes(target) {
	var id = target.id.substr(target.id.indexOf('_')+1);
	var cible = '/Clan/deleteFilter/'+id;
	execScriptServeur(traiterReponse, cible);
}

function editRes(target) {
	document.getElementById('addRessource').style.display='block';
	document.getElementsByName('frm_nom')[0].value = target.previousSibling.textContent;
	document.getElementsByName('frm_id')[0].value = target.id.substr(target.id.indexOf('_')+1);
}

function traiterReponse(xhr) {
	if (xhr.readyState==4) 
    {   
		var docXML= xhr.responseXML;
		var id = docXML.getElementsByTagName('id')[0].textContent;
		var li = document.getElementById('delete_'+id).parentElement;
		li.parentElement.removeChild(li);
		tempo();
    }
}

function filtrerTank(event) {
	tempo();
	addFiltre(event.target);
	appliquerFiltre();
	tempo();
}

function addFiltre(cible) {
	trouve = false;
	if(cible.id.substr(0,1) == 't') {
		valeur = cible.id.substr(1) ;
		if (in_array(valeur, f_tier) == true) {
			cible.style.border = '';
			cible.style.backgroundColor='';
			var filtres = new Array() 
			for(i = 0; i < f_tier.length; i++) {
				if (f_tier[i] != valeur) {
					filtres.push(f_tier[i]);
				}
			}
			f_tier = filtres;
		}
		else {
			f_tier.push(valeur);
			cible.style.border = '1px inset';
			cible.style.backgroundColor='#7db414';
		}
	}
	else {
		if (in_array(cible.id, f_type) == true) {
			cible.style.border = '';
			cible.style.backgroundColor='';
			var filtres = new Array() 
			for(i = 0; i < f_type.length; i++) {
				if (f_type[i] != cible.id) {
					filtres.push(f_type[i]);
				}
			}
			f_type = filtres;
		}
		else {
			f_type.push(cible.id);
			cible.style.border = '1px inset';
			cible.style.backgroundColor='#7db414';
		}
	}
}

function appliquerFiltre() {
	var filtres_tank = document.getElementById('filtres_tank');
	var tankFiltres = document.getElementById('tankFiltres');
	if (tankFiltres != null) {
		filtres_tank.removeChild(tankFiltres);
	}
	if(f_type.length == 0 && f_tier.length == 0) {
		return false;
	}
	var divTank = document.createElement('div');
	divTank.id = 'tankFiltres';
	for (var tankId in lstTanks){
		if ((in_array(lstTanks[tankId].type, f_type) == true || f_type.length == 0) && (in_array(lstTanks[tankId].level, f_tier) == true || f_tier.length == 0)) {
			var addTank = document.createElement('div');
			addTank.className = 'teamTank';
			var imgTank = document.createElement('img');
			imgTank.id = lstTanks[tankId].tank_id;
			imgTank.alt = lstTanks[tankId].name;
			imgTank.title = lstTanks[tankId].name;
			imgTank.src = lstTanks[tankId].image;
			imgTank.className = 'minitank '+lstTanks[tankId].nation;
			imgTank.addEventListener('click', function (event) {switchTank(event)}, false);
			addTank.appendChild(imgTank);
			divTank.appendChild(addTank);
		}
	}
	filtres_tank.appendChild(divTank);
}

function switchTank(event) {
	if (event.target.parentElement.parentElement.id == 'tankFiltres') {
		var source = document.getElementById('tankFiltres');
		var destination = document.getElementById('SelectedTanks');
	}
	else {

		var source = document.getElementById('SelectedTanks');
		var destination = document.getElementById('tankFiltres');
	}
	source.removeChild(event.target.parentElement);
	destination.appendChild(event.target.parentElement);
}

function resetFilters() {
	document.getElementById('SelectedTanks').innerHTML = null;
	document.getElementById('tankFiltres').innerHTML = null;
}

function setInputTank() {
	var lstSelectedTanks = document.getElementById('SelectedTanks');
	var selectedTanks = new Array();
	for(i = 0; i < lstSelectedTanks.children.length; i++) {
		selectedTanks.push(lstSelectedTanks.children[i].children[0].id);
	}
	document.getElementsByName('frm_tanks')[0].value = selectedTanks.toString();
}